package com.github.bridge.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.plumelog.core.PlumeLogTraceIdInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    @Autowired(required = false)
    private IgnoreProperties ignoreProperties;

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
    private final AntPathMatcher pathMatcher = new AntPathMatcher();

    /**
     * 注册 Sa-Token 拦截器，打开注解式鉴权功能
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        List<String> url = ignoreProperties.getUrl();
        url.add("/favicon.ico");
        url.add("/**/doc.html");
        url.add("/**/css/**");
        url.add("/**/js/**");
        url.add("/**/v3/**");
        url.add("/**/public/**");
        url.add("/swagger-resources/**");
        url.add("/v2/api-docs");
        url.add("/plumelog/**");
        url.add("/**/public/**");
        url.add("/config.html");
        url.add("/webjars/**");
        url.add("/**/*.css");
        url.add("/**/*.js");
        url.add("/**/*.html");

        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor(handle ->
                        StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns(ignoreProperties.getUrl());
        registry.addInterceptor(new PlumeLogTraceIdInterceptor());
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        // 就是这句addResourceLocations，加上静态文件访问路径
//        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
//    }

}
